package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public List<NursingLevelVo> queryLevelList() {
        return nursingLevelMapper.selectLevelList();
    }

    @Override
    public void addNursingLevel(NursingLevelDto nursingLevelDto) {
        int count = nursingLevelMapper.selectCount(nursingLevelDto.getName());
        if (count > 0) {
            throw new BaseException(BasicEnum.DUPLICATE_NURSING_LEVEl_NAME);
        }
        NursingLevel nursingLevel = BeanUtil.copyProperties(nursingLevelDto, NursingLevel.class);
        nursingLevel.setCreateBy(1671403256519078138L);
        nursingLevel.setUpdateBy(1671403256519078138L);
        nursingLevelMapper.insert(nursingLevel);
    }

    @Override
    public PageResponse selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevel> page = nursingLevelMapper.selectPageByNameAndStatus(name, status);
        return PageResponse.of(page, NursingLevel.class);
    }

    @Override
    public NursingLevelVo selectById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.selectById(id);
        return BeanUtil.copyProperties(nursingLevel, NursingLevelVo.class);
    }

    @Override
    public void updateNursingLevel(NursingLevelDto nursingLevelDto) {
          NursingLevel nursingLevel = BeanUtil.copyProperties(nursingLevelDto, NursingLevel.class);
          nursingLevelMapper.update(nursingLevel);
    }

    @Override
    public void deleteById(Long id) {
        nursingLevelMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id, status);
    }
}
